home *** CD-ROM | disk | FTP | other *** search
/ 3D GFX / 3D GFX.iso / amiutils / i_l / irit5 / cagd_lib / cagdruld.c < prev    next >
C/C++ Source or Header  |  1995-12-30  |  7KB  |  175 lines

  1. /******************************************************************************
  2. * CagdRuld.c - Ruled srf operator out of given two profiles.              *
  3. *******************************************************************************
  4. * Written by Gershon Elber, May. 91.                          *
  5. ******************************************************************************/
  6.  
  7. #include "cagd_loc.h"
  8.  
  9. /*****************************************************************************
  10. * DESCRIPTION:                                                               M
  11. * Constructs a ruled surface between the two provided curves.                M
  12. * OtherOrder and OtherLen (equal for Bezier) specifies the desired order and M
  13. * refineness level (if Bspline) of the other ruled direction.             M
  14. *                                                                            *
  15. * PARAMETERS:                                                                M
  16. *   Crv1, Crv2: The two curves to form a ruled surface in between.           M
  17. *   OtherOrder: Usually two, but one can specify higher orders in the ruled  M
  18. *               direction. OtherOrder must never be larger than OrderLen.    M
  19. *   OtherLen:   Usually two control points in the ruled direction which      M
  20. *               necesitates a linear interpolation.                 M
  21. *                                                                            *
  22. * RETURN VALUE:                                                              M
  23. *   CagdSrfStruct *:  The rule surface.                                      M
  24. *                                                                            *
  25. * KEYWORDS:                                                                  M
  26. *   CagdRuledSrf, ruled surface, surface constructors                        M
  27. *****************************************************************************/
  28. CagdSrfStruct *CagdRuledSrf(CagdCrvStruct *Crv1,
  29.                 CagdCrvStruct *Crv2,
  30.                 int OtherOrder,
  31.                 int OtherLen)
  32. {
  33.     CagdSrfStruct *Srf;
  34.     int i, j, k, MaxCoord, Len;
  35.     CagdPointType PType;
  36.     CagdBType IsNotRational;
  37.     CagdRType **SrfPoints, *Crv1SrcPt, *Crv2SrcPt, *SrfDestPt, t, t1,
  38.                          **Crv1Points, **Crv2Points;
  39.  
  40.     Crv1 = CagdCrvCopy(Crv1);
  41.     Crv2 = CagdCrvCopy(Crv2);
  42.  
  43.     CagdMakeCrvsCompatible(&Crv1, &Crv2, TRUE, TRUE);
  44.  
  45.     MaxCoord = CAGD_NUM_OF_PT_COORD(Crv1 -> PType),
  46.     Len = Crv1 -> Length;
  47.     PType = Crv1 -> PType;
  48.     IsNotRational = !CAGD_IS_RATIONAL_CRV(Crv1);
  49.     Crv1Points = Crv1 -> Points;
  50.     Crv2Points = Crv2 -> Points;
  51.  
  52.     switch (Crv1 -> GType) {
  53.     case CAGD_CBEZIER_TYPE:
  54.         Srf = BzrSrfNew(Len, OtherLen, PType);
  55.         break;
  56.     case CAGD_CBSPLINE_TYPE:
  57.         Srf = BspPeriodicSrfNew(Len, OtherLen, Crv1 -> Order, OtherOrder,
  58.                     Crv1 -> Periodic, FALSE, PType);
  59.         CAGD_GEN_COPY(Srf -> UKnotVector, Crv1 -> KnotVector,
  60.               sizeof(CagdRType) * (CAGD_CRV_PT_LST_LEN(Crv1) +
  61.                            Crv1 -> Order));
  62.         BspKnotUniformOpen(OtherLen, OtherOrder, Srf -> VKnotVector);
  63.         break;
  64.     case CAGD_CPOWER_TYPE:
  65.         CAGD_FATAL_ERROR(CAGD_ERR_POWER_NO_SUPPORT);
  66.         return NULL;
  67.     default:
  68.         CAGD_FATAL_ERROR(CAGD_ERR_UNDEF_CRV);
  69.         return NULL;
  70.     }
  71.  
  72.     /* Copy the control mesh - first row is exactly the same as the first    */
  73.     /* curve while last row is the same as second curve.             */
  74.     /* The middle rows are convex blend of the first/last rows.             */
  75.     SrfPoints = Srf -> Points;
  76.  
  77.     for (i = IsNotRational; i <= MaxCoord; i++)               /* First row. */
  78.     CAGD_GEN_COPY(SrfPoints[i], Crv1Points[i],
  79.               sizeof(CagdRType) * Len);
  80.  
  81.     /* Make a copy of the last row. */
  82.     for (i = IsNotRational; i <= MaxCoord; i++)                /* Last row. */
  83.     CAGD_GEN_COPY(&SrfPoints[i][Len * (OtherLen - 1)], Crv2Points[i],
  84.               sizeof(CagdRType) * Len);
  85.  
  86.     /* And compute the internal rows, if any: */
  87.     for (j = 1; j < OtherLen - 1; j++) {
  88.     t = ((CagdRType) j) / (OtherLen - 1);
  89.     t1 = 1.0 - t;
  90.     for (i = IsNotRational; i <= MaxCoord; i++) {
  91.         SrfDestPt = &SrfPoints[i][Len * j];
  92.         Crv1SrcPt = Crv1Points[i];
  93.         Crv2SrcPt = Crv2Points[i];
  94.         for (k = 0; k < Len; k++)
  95.         SrfDestPt[k] = t1 * Crv1SrcPt[k] + t * Crv2SrcPt[k];
  96.     }
  97.     }        
  98.  
  99.     CagdCrvFree(Crv1);
  100.     CagdCrvFree(Crv2);
  101.  
  102.     return Srf;
  103. }
  104.  
  105. /*****************************************************************************
  106. * DESCRIPTION:                                                               M
  107. * Constructs a bilinear surface between the four provided points.         M
  108. *                                                                            *
  109. * PARAMETERS:                                                                M
  110. *   Pt00, Pt01, Pt10, Pt11: The four points to consturct a bilinear between. M
  111. *                                                                            *
  112. * RETURN VALUE:                                                              M
  113. *   CagdSrfStruct *:  A bilinear surface with four corners at Ptij.          M
  114. *                                                                            *
  115. * KEYWORDS:                                                                  M
  116. *   CagdBilinearSrf, Bilinear surface, surface constructors                  M
  117. *****************************************************************************/
  118. CagdSrfStruct *CagdBilinearSrf(CagdPtStruct *Pt00,
  119.                    CagdPtStruct *Pt01,
  120.                    CagdPtStruct *Pt10,
  121.                    CagdPtStruct *Pt11)
  122. {
  123.     CagdCrvStruct
  124.     *Crv1 = CagdMergePtPt(Pt00, Pt01),
  125.     *Crv2 = CagdMergePtPt(Pt10, Pt11);
  126.     CagdSrfStruct
  127.     *Srf = CagdRuledSrf(Crv1, Crv2, 2, 2);
  128.  
  129.     CagdCrvFree(Crv1);
  130.     CagdCrvFree(Crv2);
  131.  
  132.     return Srf;
  133. }
  134.  
  135. /*****************************************************************************
  136. * DESCRIPTION:                                                               M
  137. * Promotes a curve to a surface by creating a ruled surface between the      M 
  138. * curve to itself. Dir controls if the curve should be U or V surface        M
  139. * direction.                                                                 M
  140. *   The resulting surface is degenerate in that its speed is zero in the     M
  141. * ruled direction and hence thesurface is not regular.                       M
  142. *                                                                            *
  143. * PARAMETERS:                                                                M
  144. *   Crv:      A Crv to promote into a surface                                M
  145. *   Dir:      Direction of ruling. Either U or V.                            M
  146. *                                                                            *
  147. * RETURN VALUE:                                                              M
  148. *   CagdSrfStruct *: The surface promoted from Crv.                          M
  149. *                                                                            *
  150. * KEYWORDS:                                                                  M
  151. *   CagdPromoteCrvToSrf                                                      M
  152. *****************************************************************************/
  153. CagdSrfStruct *CagdPromoteCrvToSrf(CagdCrvStruct *Crv, CagdSrfDirType Dir)
  154. {
  155.     CagdSrfStruct *TSrf,
  156.     *Srf = CagdRuledSrf(Crv, Crv, 2, 2);
  157.  
  158.     switch (Dir) {
  159.     case CAGD_CONST_U_DIR:
  160.         break;
  161.     case CAGD_CONST_V_DIR:
  162.         TSrf = CagdSrfReverse2(Srf);
  163.         CagdSrfFree(Srf);
  164.         Srf = TSrf;
  165.         break;
  166.     default:
  167.         CAGD_FATAL_ERROR(CAGD_ERR_DIR_NOT_CONST_UV);
  168.         break;
  169.     
  170.     }
  171.     return Srf;
  172. }
  173.  
  174.  
  175.